// 获取到新闻ID
const newsId = getUrlParam('id');
// console.log(newsId)
//获取登录状态
//解码decodeURIComponent
let userInfo = getCookie('userInfo') ? JSON.parse(decodeURIComponent(getCookie('userInfo'))) : null;
// console.log(userInfo)

if (userInfo) {     // 登录状态
    //登录按钮状态
    document.querySelector('.resgist').innerHTML = `${userInfo.nickname} 欢迎`;
    //退出按钮
    document.querySelector('.out').addEventListener('click', () => {
        //删除cookie
        removeCookie('userInfo');
        //重新加载
        location.reload();
    })
    //发布评论按钮
    document.querySelector('button').innerHTML = `发布评论`
} else {

    //登录按钮
    document.querySelector('.resgist').innerHTML = '登录';
    document.querySelector('.resgist').onclick = () => {
        location.href = 'login.html?callback=' + encodeURIComponent(location.href);
    }
    //发布评论按钮
    document.querySelector('button').innerHTML = `登录后发布评论`;
    document.querySelector('button').onclick = () => {
        location.href = 'login.html?callback=' + encodeURIComponent(location.href);
    }
}

window.addEventListener('load', async () => {
    // 发送ajax请求获取新闻详情并渲染页面
    let res = await axios.post(
        'http://39.98.112.34:8000/introduction/detail',
        { id: newsId }
    )
    let response = res.data.data
    console.log(response)
    let html = '';
    html = `<h1>${response.title}</h1>
    <img src="${response.coverImg}" alt="">
    ${response.content}
    <span>发布时间：${response.createAt}</span>
    `
    document.querySelector('.content').innerHTML = html;
    // 赞
    document.querySelector('.icon-zan1').innerHTML = response.goodCount;
    // 踩
    document.querySelector('.icon-zan').innerHTML = response.downCount


    document.querySelector('.count').addEventListener('click', (e) => {
        if (userInfo) {
            if (e.target.className == 'iconfont icon-zan1') {  //赞
                // ajax请求获取点赞详情
                axios({
                    method: 'post',
                    url: 'http://39.98.112.34:8000/introduction/good',
                    data: { id: newsId },
                    headers: { 'Authorization': 'bearer ' + userInfo.sessionKey }
                })
                document.querySelector('.icon-zan1').innerHTML = response.goodCount + 1;
                document.querySelector('.icon-zan1').style.color = 'red';
            }
            if (e.target.className == 'iconfont icon-zan') {  //踩
                // ajax请求获取踩详情
                axios({
                    method: 'post',
                    url: 'http://39.98.112.34:8000/introduction/down',
                    data: { id: newsId },
                    headers: { 'Authorization': 'bearer ' + userInfo.sessionKey }
                })
                document.querySelector('.icon-zan').innerHTML = response.goodCount + 1;
                document.querySelector('.icon-zan').style.color = 'red';
            }
        } else {
            layer.msg('请先登录，才能点赞哦！', {
                icon: 5
            });
        }

    })
})



